﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using Caliburn.Micro;
using Microsoft.Phone.Tasks;
using Wulong.Phone.Qiushi.Helpers;
using Wulong.Phone.Qiushi.i18n;
using Wulong.Phone.Qiushi.Models;
using Wulong.Phone.Utils.HttpHelpers;

namespace Wulong.Phone.Qiushi.ViewModels
{
    public class NewPostViewModel : ViewModelBase, IHandle<TaskCompleted<PhotoResult>>
    {

        public NewPostViewModel(IEventAggregator events, INavigationService navigationService)
        {
            _events = events;
            _navigationService = navigationService;
            CanRemovePic = false;
            IsAnonymous = true;
            IsCommentAllowed = true;
        }

        private static readonly Uri DefaultPlaceholder = new Uri("/images/placeholder.png", UriKind.Relative);
        private readonly BitmapImage _pictureSource = new BitmapImage(DefaultPlaceholder);
        public BitmapImage PictureSource
        {
            get { return _pictureSource; }
        }

        public FileParameter FileParam { get; set; }

        private string _body;
        public string Body
        {
            get { return _body; }
            set
            {
                _body = value;
                NotifyOfPropertyChange(() => Body);
            }
        }

        private string _tags;
        public string Tags
        {
            get { return _tags; }
            set
            {
                _tags = value;
                NotifyOfPropertyChange(() => Tags);
            }
        }

        private bool _isCommentAllowed;
        public bool IsCommentAllowed
        {
            get { return _isCommentAllowed; }
            set
            {
                if (value == _isCommentAllowed)
                    return;
                _isCommentAllowed = value;
                NotifyOfPropertyChange(() => IsCommentAllowed);
            }
        }

        private bool _isAnonymous;
        public bool IsAnonymous
        {
            get { return _isAnonymous; }
            set
            {
                if (value == _isAnonymous)
                    return;
                _isAnonymous = value;
                NotifyOfPropertyChange(() => IsAnonymous);
            }
        }

        private bool _canRemovePic;
        public bool CanRemovePic
        {
            get { return _canRemovePic; }
            set
            {
                if (value == _canRemovePic)
                    return;
                _canRemovePic = value;
                NotifyOfPropertyChange(() => CanRemovePic);
            }
        }

        public void Handle(TaskCompleted<PhotoResult> message)
        {
            if(message.Result.TaskResult == TaskResult.OK)
            {
                _pictureSource.SetSource(message.Result.ChosenPhoto);
                NotifyOfPropertyChange(() => PictureSource);
                FileParam = new FileParameter(message.Result.ChosenPhoto.ReadToEnd(), message.Result.OriginalFileName, "image/jpeg");
                CanRemovePic = true;
            }                
        }

        public void LaunchPhotoChooser()
        {
            _events.RequestTask<PhotoChooserTask>();
        }

        public void LaunchCamera()
        {
            _events.RequestTask<CameraCaptureTask>();
        }

        public void RemovePic()
        {
            CanRemovePic = false;
            PictureSource.UriSource = DefaultPlaceholder;
            FileParam = null;
        }

        protected override void OnActivate()
        {
            _events.Subscribe(this);
            base.OnActivate();
        }

        protected override void OnDeactivate(bool close)
        {
            _events.Unsubscribe(this);
            base.OnDeactivate(close);
        }

        public void SubmitStory()
        {
            var postParameters = new Dictionary<string, object>
                                     {
                                         {"article[group_id]", "2"},
                                         {"article[content]", Body},
                                         {"article[pciture]", FileParam},
                                         {"article[tag_line]", Tags},
                                         {"article[comment_status]", IsCommentAllowed ? "0" : "closed"},
                                         {"article[anonymous]", IsAnonymous ? "0" : "1"},
                                         {"article[commit]", AppResources.VM_NewPost_Form_Commit_Field}
                                     };
            QiushiMapper.PostQiushiArticle(postParameters)
                .Subscribe(n => MessageBox.Show(n),
                MessageHelper.ShowErrorMessage);
            _navigationService.GoBack();
        }

        public void GoBack()
        {
            _navigationService.GoBack();
        }

    }
}
